package com.tedu.charging.service.user.controller;

import com.tedu.charging.common.pojo.param.UserGetBalanceParam;
import com.tedu.charging.common.pojo.param.UserUpdateBalanceParam;
import com.tedu.charging.common.pojo.web.CodeConstant;
import com.tedu.charging.common.pojo.web.ServerResult;
import com.tedu.charging.service.user.pojo.vo.UserVO;
import com.tedu.charging.service.user.service.IChargingUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@RestController
@RequestMapping("/charge/user")
@CrossOrigin
public class UserController {
    @Autowired
    IChargingUserInfoService chargingUserInfoService;

//    @GetMapping(value = "/{userId}")
//    public AjaxResult getUserInfo(@PathVariable Integer userId){
//        UserVO userVO = chargingUserInfoService.getUser(userId);
//        return  AjaxResult.success(userVO);
//    }
@GetMapping(value = "/{userId}")
public ServerResult<UserVO> getUserInfo(@PathVariable Integer userId){
    UserVO userVO = chargingUserInfoService.getUser(userId);
    return  new ServerResult<UserVO>(0,"success",userVO);
}

    @PostMapping(value = "/getBalance")
    public ServerResult<BigDecimal> getBalance(@RequestBody UserGetBalanceParam param){
        UserVO userVO = chargingUserInfoService.getUser(param.getUserId());
        BigDecimal balance = userVO.getBalance();
        return  new ServerResult<BigDecimal>(0,"success",balance);
    }

    @PostMapping(value = "/upDateBalance")
    //难点在于安全问题
    public ServerResult<String> upDateBalance(@RequestBody UserUpdateBalanceParam userUpdateBalanceParam){
        boolean isSuccess = chargingUserInfoService.updateBalance(userUpdateBalanceParam.getUserId(), userUpdateBalanceParam.getBalance());
       if (isSuccess){
           return  new ServerResult<String>(0,"success","sucess");
       }else{
           return  new ServerResult<String>(CodeConstant.UPDATE_BALANCE_FAIL,"更新余额失败","fail");
       }

    }
}
